using System;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using MCromwell.StaffIntranet.Presenters;
using MCromwell.StaffIntranet.Task;
using MCromwell.StaffIntranet.DTO;
using System.Text;

namespace MCromwell.StaffIntranet.UI
{
    public partial class Search : Page, IStaffSearchingView, IStaffDeletionView
    {

        public event EventHandler<StaffSearchingInitializeEventArgs> Initialize;
        private StaffSearchingPresenter searchingPresenter;
        private StaffDeletionPresenter deletionPresenter;
        private const int PAGE_SIZE = 5;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            searchingPresenter = new StaffSearchingPresenter(this);
            deletionPresenter = new StaffDeletionPresenter(this);

            if (!IsPostBack)
                RaiseInitialize();
            
            BindEvents();
        }

        private void RaiseInitialize()
        {
            if (Initialize != null)
                Initialize(this, new StaffSearchingInitializeEventArgs(LoginManager.RetrieveCurrentSessionIdentifer()));
        }

        private void BindEvents()
        {
            btnSearch.Click += delegate
            {
                grdStaffFound.DataBind();
            };
            
        }

        private string LastName
        {
            get
            {
                return Request.QueryString["last"];
            }
        }

        private string FirstName
        {
            get
            {
                return Request.QueryString["first"];
            }
        }

        private int PageIndex
        {
            get
            {
                return grdStaffFound.PageIndex;
            }
        }
                
        protected void grdStaffFound_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdStaffFound.PageIndex = e.NewPageIndex;
        }

        protected void StaffDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (!e.ExecutingSelectCount)
            {
                e.InputParameters.Add("presenter", searchingPresenter);
            }
        }
        
        public void DisplayAvailable(IList<LocationDTO> locations)
        {
            ListItem wildcardItem = new ListItem("<All>", "-1");
            ddlLocations.Items.Add(wildcardItem);
            
            foreach (LocationDTO location in locations)
            {
                ListItem listItem = new ListItem(location.Value, location.Id.ToString());
                ddlLocations.Items.Add(listItem);
            }
        }
        
        public bool CanEditStaff
        {
            set 
            {
                grdStaffFound.Columns[8].Visible = value;
                grdStaffFound.Columns[9].Visible = value;
            }
        }

        protected void grdStaffFound_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                StaffDTO staff = (StaffDTO)e.Row.DataItem;
                LinkButton deleteButton = (LinkButton)e.Row.FindControl("DeleteButton");
                deleteButton.CommandArgument = staff.Id.ToString();
            }
        }

        protected void StaffDataSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.InputParameters.Add("presenter", deletionPresenter);
        }

    }
}
